package com.test.daily.leetcode.y2022.m05.day0524.v06;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @descriptions:
 * @author: Tom
 * @date: 2022/5/24 下午 08:23
 * @version: 1.0
 */
public class Solution {
    public static class Program {
        int start;
        int end;

        public Program(int start, int end) {
            this.start = start;
            this.end = end;
        }
    }

    public static int bestRange1(Program[] programs) {
        return process(programs, 0, 0);
    }

    public static int process(Program[] programs, int done, int timeline) {
        int max = done;
        for (int i = 0; i < programs.length; i++) {
            if (programs[i].start >= timeline) {
                max = Math.max(max, process(programs, done + 1, programs[i].end));
            }
        }
        return max;
    }

    public static Program[] copyButExcept(Program[] programs, int i) {
        Program[] ans = new Program[programs.length - 1];
        int k = 0;
        for (int j = 0; j < programs.length; j++) {
            if (j != i) {
                ans[k++] = programs[j];
            }
        }
        return ans;
    }

    public static int bestRange2(Program[] programs) {
        Arrays.sort(programs, new ProgramComparator());
        int timeLine = 0;
        int done = 0;
        for (int i = 0; i < programs.length; i++) {
            if (programs[i].start > timeLine) {
                done++;
                timeLine = programs[i].end;
            }
        }
        return done;
    }

    public static class ProgramComparator implements Comparator<Program> {
        @Override
        public int compare(Program o1, Program o2) {
            return o1.end - o2.end;
        }
    }
}
